var stepMessage = function(obj, message, css){
	//remove class image
	$.map(obj, function(n){
		$("span[id='" + n + "']").removeClass('message').removeClass('error');
	});	
	//remove corresponding image
	$.map(obj, function(n){
		$("img[id='" + n + "']").attr('src', '../images/blank.jpg');			
	});	
	$.map(obj, function(n){
		$("span[id='" + n + "']").html(message).addClass(css);			
	});	
	return false;
}


var imageLoader = function(obj){
	//remove corresponding span message
	$.map(obj, function(n){
		$("span[id='" + n + "']").html('');			
	});	
	$.map(obj, function(n){
		$("img[id='" + n + "']").attr('src', "../images/rotator.gif");
	});
	return false;
}



function adTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['AD'],
		api : 'termination-api.cfm?api=ad'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) { jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				HomeFolderTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});	
}



function HomeFolderTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['HomeFolder'],
		api : 'termination-api.cfm?api=home_folder'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				HRTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}




function HRTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['HR'],
		api : 'termination-api.cfm?api=hr'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				TimeKeeperTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}




function TimeKeeperTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['TimeKeeper'],
		api : 'termination-api.cfm?api=time_keeper'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				ExportMailTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function ExportMailTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['ExportMail'],
		api : 'termination-api.cfm?api=export_mail'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				UserAccountTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function UserAccountTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['UserAccount'],
		api : 'termination-api.cfm?api=user_account'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				RosettaTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}



function RosettaTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['Rosetta'],
		api : 'termination-api.cfm?api=rosetta'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
				CompletedTermination();		
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}




function CompletedTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['Completetion'],
		api : 'termination-api.cfm?api=completion'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){
				$("#Notice").html('Termination is complete.').removeClass('error').addClass('message');
				return false;
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}



function bindElements(){
	stepMessage( ['AD', 'HomeFolder', 'HR', 'TimeKeeper', 'ExportMail', 'UserAccount', 'Rosetta'], 'Waiting...', 'error' );			
}



$(document).ready(function(){
	bindElements();
	
	if( $("#processed").text() != 'Here' ){
		setTimeout('adTermination()', 1000);
	}

});